package com.situ.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.situ.context.BaseContext;
import com.situ.domain.dto.RoleAndMenuDTO;
import com.situ.domain.entity.RoleManagerKey;
import com.situ.domain.entity.TMenu;
import com.situ.domain.vo.MenuListVO;
import com.situ.mapper.TMenuMapper;
import com.situ.mapper.TRoleMapper;
import com.situ.service.MenuService;
import com.situ.service.RoleManagerService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
@RequiredArgsConstructor
public class MenuServiceImpl extends ServiceImpl<TMenuMapper, TMenu> implements MenuService {


    private final TRoleMapper roleMapper;

    private final RoleManagerService roleManagerService;

    /**
     * 获取全部菜单列表
     * @return
     */
    @Override
    public List<MenuListVO> getAllMenuList() {
        // 获取所有的父级菜单
        List<TMenu> parentMenus = this.lambdaQuery()
                .eq(TMenu::getPid,1)
                .list();
        List<MenuListVO> list = parentMenus.stream().map(menu -> {
            List<TMenu> childrenList = this.lambdaQuery()
                    .eq(TMenu::getPid, menu.getId())
                    .list();
            List<MenuListVO.Children> childrens = childrenList.stream().map(children -> MenuListVO.Children
                    .builder()
                    .id(children.getId())
                    .title(children.getName())
                    .path(children.getPath())
                    .linkUrl(children.getLinkUrl())
                    .children(List.of())
                    .build()).toList();
            return MenuListVO
                    .builder()
                    .id(menu.getId())
                    .title(menu.getName())
                    .path(menu.getPath())
                    .icon(menu.getIcon())
                    .children(childrens)
                    .build();
        }).toList();
        return list;
    }


    /**
     * 获取当前登录用户的菜单列表
     * @return
     */
    @Override
    public Set<MenuListVO> getMenuByManager() {
        Integer currentId = BaseContext.getCurrentId();
        Set<MenuListVO> set = new TreeSet<>();
        List<RoleManagerKey> roleManagers = roleManagerService.lambdaQuery()
                .eq(RoleManagerKey::getManagerId, currentId)
                .list();
        roleManagers.forEach(roleManager -> {
            RoleAndMenuDTO roleAndMenuDTO = roleMapper.getRoleAndMenuById(Long.valueOf(roleManager.getRoleId()));
            List<TMenu> parentMenu = roleAndMenuDTO.getMenuList().stream().filter(menu -> menu.getPid() == 1).toList();
            parentMenu.forEach(menu -> {
                List<TMenu> childrenMenu = roleAndMenuDTO.getMenuList().stream().filter(children -> Objects.equals(children.getPid(), menu.getId())).toList();
                MenuListVO menuListVO = new MenuListVO();
                BeanUtil.copyProperties(menu,menuListVO);
                menuListVO.setTitle(menu.getName());
                menuListVO.setChildren(childrenMenu.stream().map(
                        children -> MenuListVO.Children.builder()
                                .id(children.getId())
                                .title(children.getName())
                                .path(children.getPath())
                                .linkUrl(children.getLinkUrl())
                                .children(List.of())
                                .build()
                ).toList());
                set.add(menuListVO);
            });
        });
        return set;
    }
}
